package com.logic.modular.game.player.bag.service;

import cn.hutool.core.bean.BeanUtil;

import com.comment.pojo.domain.bag.BagItem;
import com.comment.pojo.domain.EquipAttribute;
import com.comment.pojo.domain.logic.core.CoreProp;
import com.logic.modular.gameCore.cultivate.prop.CorePropServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 背包物品服务---武器装备
 */
@Slf4j
@Service

public class PlayerPropType4ServiceImpl {
    @Resource
    private BagItemServiceImpl bagItemService;

    /**
     * 使用装备
     *
     * @param playerId  玩家id
     * @param bagItemId 背包单元格id
     * @return 是否成功
     */
    public boolean tryEquipArms(Long playerId, Long bagItemId, Long playerHeroId) {
        BagItem bagItem = bagItemService.obtainPlayerBagItemHandlerPower(playerId, bagItemId);
        bagItem.setPropStatus(1);
        bagItem.setStatus(1);

        HashMap<String, Object> useInfo = new HashMap<>();
        useInfo.put("playerHeroId", playerHeroId);
        useInfo.put("equipSlot", 1);
        bagItem.extend.put("useInfo", useInfo);
        return bagItemService.updateById(bagItem);
    }

    /**
     * 卸下装备
     *
     * @param playerId  玩家id
     * @param bagItemId 背包单元格id
     * @return 是否成功
     */
    public boolean tryUnEquipArms(Long playerId, Long bagItemId, Long playerHeroId) {
        BagItem bagItem = bagItemService.obtainPlayerBagItemHandlerPower(playerId, bagItemId);
        if (bagItem.getPropStatus() == 1) {
            bagItem.setPropStatus(0);
            bagItem.setExtend(new HashMap<>());
        }
        return bagItemService.updateById(bagItem);
    }

    public BagItem selectById(Long id) {
        return bagItemService.getById(id);
    }

    /**
     * 获取装备属性
     *
     * @param playerId  玩家id
     * @param bagItemId 背包id
     * @return 装备属性
     */
    public EquipAttribute obtainEquipAttribute(Long playerId, Long bagItemId) {
        BagItem bagItem = bagItemService.getById(bagItemId);
        if (bagItem == null) {
            throw new RuntimeException("背包单元格不存在");
        }
        if (!bagItem.getUid().equals(playerId)) {
            throw new RuntimeException("玩家参数异常");
        }
        CoreProp cache = CorePropServiceImpl.getCache(bagItem.getCorePropId());
        EquipAttribute bean = BeanUtil.toBean(cache, EquipAttribute.class);
        int slotType = 0;
        if (cache.getType() == 4) {
            slotType = 1;
        }
        if (cache.getType() == 5) {
            slotType = 2;
        }
        bean.setSlot(slotType);
        bean.setId(bagItem.getId());
        return bean;
    }

}
